<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="shortcut icon" href="img/logo.ico" type="image/x-icon">
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <script src="js/jquery.min.js"></script>
</head>

<body>
<!-- 导航栏 -->
<div class="nav">
    <img src="img/logo.jpeg" alt="">
    <span class="title">我的博客系统</span>
    <!-- 用来占据中间位置 -->
    <span class="spacer1"></span>
    <a href="login.html">登录</a>
    <a href="reg.html">注册</a>
</div>
<!-- 版心 -->
<div class="login-container">
    <!-- 中间的登陆框 -->
    <div class="login-dialog">
        <h3 id="reg">注册</h3>
        <div class="row1">
            <input type="text" placeholder="昵称" id="nickname">
        </div>
        <div class="row1">
            <input type="text" placeholder="用户名" id="username">
        </div>
        <div class="row1">
            <input type="password" placeholder="密码" id="password">
        </div>
        <div class="row1">
            <input type="password" placeholder="确认密码" id="password2">
        </div>
        <div class="row1">
            <button id="submit" onclick="mySubmit()">注册</button>
        </div>
    </div>
</div>
<script>
    function replaceStr(a) {
        a = a.replace(/(<br[^>]*>| |\s*)/g, '')
            .replace(/\&/g, "&amp;")
            .replace(/\"/g, "&quot;")
            .replace(/\'/g, "&#39;")
            .replace(/\</g, "&lt;")
            .replace(/\>/g, "&gt;");
        return a;
    }

    jQuery(document).keyup(function (event) {
        if (event.keyCode == 13) {
            jQuery("#submit").click();
        }
    });

    // 注册数据的提交
    function mySubmit() {
        var nickname = jQuery("#nickname");
        var username = jQuery("#username");
        var password = jQuery("#password");
        var password2 = jQuery("#password2");
        // 1.对输入的内容进行非空和正确性校验
        if (jQuery.trim(nickname.val()) == "") {
            alert("请输入昵称！");
            nickname.focus();
            return;
        }
        if (jQuery.trim(username.val()) == "") {
            alert("请输入用户名！");
            username.focus();
            return;
        }
        if (jQuery.trim(password.val()) == "") {
            alert("请输入密码！");
            password.focus();
            return;
        }
        if (jQuery.trim(password2.val()) == "") {
            alert("请确认密码！");
            password2.focus();
            return;
        }
        // 昵称长度检验
        if (nickname.val().length > 8) {
            alert("昵称最长为8个字符");
            nickname.focus();
            return;
        }
        // 密码强度校验
        if (password.val().length < 8) {
            alert("密码长度至少为8");
            password.focus();
            return;
        }
        if (password.val() != password2.val()) {
            alert("两次密码不一致！");
            password.focus();
            return;
        }
        // 2.发送ajax请求到后端
        jQuery.ajax({
            url: "reg",
            type: "POST",
            data: {
                "nickname": replaceStr(nickname.val()),
                "username": username.val(),
                "password": password.val()
            },
            success: function (data) {
                if (data.data != null && data.data == 1) {
                    alert("注册成功！");
                    // 跳转到登录页面
                    location.href = "login.html";
                } else {
                    alert("注册失败！");
                }
            }
        });
    }
</script>
</body>

</html>